package com.bignerdranch.android.furnitureapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bignerdranch.android.furnitureapplication.adapter.FurnitureInfoAdapter;
import com.bignerdranch.android.furnitureapplication.entity.FurnitureInfoBean;
import com.bignerdranch.android.furnitureapplication.net.RetrofitHelper;
import com.bignerdranch.android.furnitureapplication.widgets.AutoScrollViewPager;
import com.bignerdranch.android.furnitureapplication.widgets.BaseViewPagerAdapter;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class FurnitureBuyActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private AutoScrollViewPager mScrollViewPager;
    private List<FurnitureInfoBean> dataList;
    private List<String> paths;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_furniture_buy);

        initViews();
        getFurnitureInfo();
    }

    private void initViews() {
        mRecyclerView = findViewById(R.id.furniture_buy_recycler);
        mScrollViewPager = findViewById(R.id.view_pager_image);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mScrollViewPager.onDestroy();
    }

    private BaseViewPagerAdapter.OnAutoViewPagerItemClickListener listener = new BaseViewPagerAdapter.OnAutoViewPagerItemClickListener<String>() {

        @Override
        public void onItemClick(int position, String url) {
            Intent intent = new Intent(FurnitureBuyActivity.this, BuyFurnitureActivity.class);
            intent.putExtra("furn_info", dataList.get(position));
            startActivity(intent);
        }
    };

    private void getFurnitureInfo() {
        RetrofitHelper.getNewsService()
                .getFurnitureInfo()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<FurnitureInfoBean>>() {
                    @Override
                    public void onCompleted() {
                        FurnitureInfoAdapter furnitureInfoAdapter = new FurnitureInfoAdapter(dataList);
                        paths = new ArrayList<>();
                        for (int i = 0; i < 4; i++) {
                            paths.add(dataList.get(i).getRedwood_image());
                        }
                        BaseViewPagerAdapter<String> adapter = new BaseViewPagerAdapter<String>(FurnitureBuyActivity.this, listener) {
                            @Override
                            public void loadImage(ImageView imageView, int i, String s) {
                                Glide.with(FurnitureBuyActivity.this).load(s).into(imageView);
                            }

                            @Override
                            public void setSubTitle(TextView textView, int position, String s) {
                                textView.setText(null);
                            }
                        };
                        mScrollViewPager.setAdapter(adapter);
                        adapter.add(paths);
                        mRecyclerView.setAdapter(furnitureInfoAdapter);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(List<FurnitureInfoBean> furnitureInfoBeans) {
                        dataList = furnitureInfoBeans;
                    }
                });
    }
}
